<template>
    <div>
        <!--卡片-->
        <el-card class="box-card">
            <!--定义面包屑-->
            <el-breadcrumb separator=">">
                <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>营销管理</el-breadcrumb-item>
                <el-breadcrumb-item>广告管理</el-breadcrumb-item>
            </el-breadcrumb>
            <el-divider></el-divider>
            <!--表单的搜索框
                 :model="双向绑定表单的数据"
            -->
            <el-form :inline="true" :model="advertiseFormData"  class="demo-form-inline">
                <el-form-item >
                    <el-input v-model="advertiseFormData.name" placeholder="请输入广告名称"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="select">查询</el-button>
                    <el-button type="primary" @click="insert">添加</el-button>
                </el-form-item>
            </el-form>

            <!--数据表格-->
            <template>
                <el-table
                        :data="advertisetableData"
                        :border="true"
                        stripe
                        style="width: 100%;font-size: 20px">
                    <el-table-column
                            prop="advertiseid"
                            label="编号"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="name"
                            label="广告名称"
                            width="180">
                    </el-table-column>
                    <el-table-column prop="Pic" label="图片" width="200">
                        <template v-slot="scope">
                            <img :src="scope.row.pic" alt="" width="180" height="120">
                        </template>
                    </el-table-column>
                    <el-table-column
                            prop="pname"
                            label="广告位置"
                            width="180">
                    </el-table-column>
                    <el-table-column
                            prop="startTime"
                            label="开始时间">
                    </el-table-column>
                    <el-table-column
                            prop="endTime"
                            label="结束时间">
                    </el-table-column>

                    <el-table-column
                            prop="clickCount"
                            label="点击量">
                    </el-table-column>

                    <el-table-column prop="status" label="状态:上线/下线">
                        <template slot-scope="scope">
                            <el-switch
                                    v-model="scope.row.status"
                                    active-color="#13ce66"
                                    :active-value="0"
                                    :inactive-value="1"
                                    inactive-color="#ff4949"
                                    @change="switchStatus(scope.row.status,scope.row.advertiseid)">
                            </el-switch>
                        </template>
                    </el-table-column>

                    <el-table-column
                            prop="note"
                            label="备注">
                    </el-table-column>

                    <el-table-column label="操作">
                        <template slot-scope="scope">
<!--                            编辑-->
                            <el-button type="primary" icon="el-icon-edit" circle @click="selectById(scope.row.advertiseid)"></el-button>
<!--                            删除-->
                            <el-button type="danger" icon="el-icon-delete" circle @click="deleteById(scope.row.advertiseid)"></el-button>
                        </template>
                    </el-table-column>
                </el-table>

                <!--分页-->
                <el-pagination
                        @size-change="handleSizeChange"
                        @current-change="handleCurrentChange"
                        :current-page="currentPage"
                        :page-sizes="[5, 10, 15, 20]"
                        :page-size="pageSize"
                        layout="total, sizes, prev, pager, next, jumper"
                        :total="total">
                </el-pagination>
            </template>

            <div>
                <el-dialog
                        title="添加"
                        :visible.sync="addAdvertiseForm"
                        width="30%">
                <span>
                    <el-form label-position="left" label-width="80px" :model="addAdvertise"  :rules="rules" ref="FromRef">
                          <el-form-item label="编号" v-if="show">
                        <el-input v-model="addAdvertise.advertiseid"></el-input>
                      </el-form-item>
                      <el-form-item label="广告名称" prop="name">
                        <el-input v-model="addAdvertise.name"></el-input>
                      </el-form-item>
                      <el-form-item label="开始时间" prop="startTime">
                        <el-input type="date" v-model="addAdvertise.startTime"></el-input>
                      </el-form-item>
                      <el-form-item label="结束时间" prop="endTime">
                        <el-input type="date" v-model="addAdvertise.endTime"></el-input>
                      </el-form-item>


                   <el-form-item label="广告位置" prop="placeid">
                      <el-select v-model="addAdvertise.placeid" style="width: 100%"  placeholder="请选择广告位置">
                        <el-option
                                 v-for="item in options"
                                :key="item.placeid"
                                :label="item.pname"
                                :value="item.placeid">
                            <span style="float: left;color: #8492a6; font-size: 13px">{{ item.pname }}</span>
                            <span style="float: right; color: #8492a6; font-size: 13px">{{ item.placeid}}</span>
                        </el-option>
                    </el-select>
                   </el-form-item>


                     <el-form-item label="状态" prop="status">
                         <el-radio style="float: left" v-model="addAdvertise.status" label="0">上线</el-radio>
                        <el-radio  style="float: left" v-model="addAdvertise.status" label="1">下线</el-radio>
                      </el-form-item>

                      <el-form-item label="备注">
                        <el-input v-model="addAdvertise.note"></el-input>
                      </el-form-item>

                    </el-form>
                </span>
                    <span slot="footer" class="dialog-footer">
                    <el-button @click="addAdvertiseForm= false">取 消</el-button>
                    <el-button type="primary" @click="confirmAdd">确 定</el-button>
                </span>
                </el-dialog>
            </div>
        </el-card>
    </div>
</template>

<script>
    export default {
        name: "Advertise",
        data(){
            return {
                //条件搜索对象
                advertiseFormData:{},

                //表格数据
                advertisetableData:[],

                //隐藏id
                show:false,

                options:[],


                rules:{
                    name:[
                        { required: true, message: '请输入名称', trigger: 'blur' },
                    ],
                    startTime:[
                        { required: true, message: '请选择开始时间', trigger: 'blur' },
                    ],
                    endTime:[
                        { required: true, message: '请选择结束时间', trigger: 'blur' },
                    ],
                    placeid:[
                        {required: true, message: '广告位置不能为空', trigger: 'blur'}
                    ],
                    status:[
                        {required: true, message: '是否上线', trigger: 'blur'}
                    ],

                },

                //每页显示的条数
                pageSize: 5,
                //当前的显示的页码
                currentPage:1,
                //总条数
                total:0,

                addAdvertise: {},

                //表单数据
                addAdvertiseForm:false,

            }
        },
        created() {
            //初始化菜单
            this.initAdvertiseTable()
            //this.selectPlace()
        },
        methods:{
            //初始化列表
            initAdvertiseTable(){
                var that=this;
                this.$axios.post(`clms-market/market/Advertise/initAdvertiseTable/${this.currentPage}/${this.pageSize}`,this.advertiseFormData).then(function (resp) {
                    that.advertisetableData=resp.data.result.records;
                    that.total=resp.data.result.total;
                    that.options=resp.data.result.records;
                })

            },

            //添加和修改
            confirmAdd(){
                var that=this;

                this.$refs.FromRef.validate((valid) => {
                    if (valid) {
                        if(this.addAdvertise.advertiseid!=null){
                            this.$axios.put("clms-market/market/Advertise/update",this.addAdvertise).then(function (result) {
                                that.addAdvertiseForm=false;
                                that.addAdvertise={};
                                that.initAdvertiseTable();
                            })
                        }else {
                            this.$axios.post("clms-market/market/Advertise/insert", this.addAdvertise).then(function (result) {
                                that.addAdvertiseForm = false;
                                that.addAdvertise={};
                                that.initAdvertiseTable();
                            })
                        }

                    } else {
                        console.log('error submit!!');
                        return false;
                    }
                });


            },

            //条件查询按钮触发
            select(){
                this.initAdvertiseTable();
            },

            //添加按钮触发
            insert(){
                this.addAdvertiseForm=true;
                this.addAdvertise={};
            },


            //根据id查询回显
            selectById(id){
                var that=this;
                this.$axios.get(`clms-market/market/Advertise/selectById/${id}`).then(function (returnResult) {
                    that.addAdvertiseForm=true;
                    that.addAdvertise=returnResult.data.result.huixian;
                    console.log(returnResult)
                })
            },

            //删除
            deleteById(id){
                var that=this;
                this.$confirm('确认删除此行?', '提示',{
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    this.$axios.delete("clms-market/market/Advertise?id="+id).then(function (result)  {
                        that.initAdvertiseTable();

                    })
                })

            },

            //当改变每页显示的条数时触发该方法
            handleSizeChange(v){
                this.pageSize=v;
                this.initAdvertiseTable();

            },
            //切换页码时触发该方法
            handleCurrentChange(v){
                this.currentPage=v;
                this.initAdvertiseTable();
            },

            switchStatus(status,advertiseid){
                //改变状态
                var that=this;
                this.$axios.post(`clms-market/market/Advertise/updateStatus/${status}/${advertiseid}`).then(function (resp) {
                    that.initAdvertiseTable();
                })
            },


            //搜索place
            // selectPlace(){
            //     var that=this;
            //     this.$axios.post("clms-market/market/Advertise/selectPlace", this.addAdvertise).then(function (resp) {
            //         console.log(resp.data)
            //        that.options=resp.data;
            //
            //
            //     })
            // },


        }
    }
</script>
<style scoped>
</style>